﻿'// ========================================================================= '
'// ------------------------------------------------------------------------- '
'//!				\file		CST_Grid.vb
'			        \date		june 2010
'			        \author		TNick
'
'			        \brief		Dialog for grid settings
'
'
'   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'   This file is considered universal knowledge. No restrictions
'   are imposed; use as you see fit.
'   Please read ReadMe.txt and Licence.txt in root folder
'   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'
'   
'
'// ------------------------------------------------------------------------- '
'// ========================================================================= '
'//
'//
'//
'//
'//  IMPORTS     ------------------------------------------------------------ '
#Region "Imports"

Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.Geometry
Imports FSK_XL
Imports Autodesk.AutoCAD.EditorInput

#End Region
'//  IMPORTS     ============================================================ '
'//
'//
'//
'//
'//  DEFINITIONS    --------------------------------------------------------- '




'//  DEFINITIONS    ========================================================= '
'//
'//
'//
'//
'//  DATA    ---------------------------------------------------------------- '


'//  DATA    ================================================================ '
'//
'//
'//
'//
'//  CLASS    --------------------------------------------------------------- '
Public Class CD_PolyPt
    '// --------------------------------------------------------------------- '
    '// Data
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Function GetPointList (ByRef pts() As FSK_PT3D) As Integer

        Dim sAllText As string = Me.TX_ListOfPoints.Text  & vbNewLine

        Dim sep(0 To 0) As String
        sep(0) = vbNewLine
        Dim lines() As String = sAllText.Split(sep, _
                                    StringSplitOptions.RemoveEmptyEntries)
        If UBound(lines) < 0 then
            Return 0
        End If
        ReDim pts(0 to UBound(lines))
        sep(0) = vbTab
        For i As Integer = 0 to UBound(lines)
            Dim words() As String
            words = lines(i).Split(sep, _
                          StringSplitOptions.RemoveEmptyEntries)
            If UBound(words) <> 3 And UBound(words) <> 4 then
                MsgBox(lines(i) & vbNewLine & _
                       CMain._T(tx.PT3D_ERPRS), _
                       MsgBoxStyle.Critical or MsgBoxStyle.ApplicationModal, _
                       CMain._T(tx.ERR))
                Return 0
            End If
            Try
                If Me.CK_WithNr.Checked = True Then
                    pts(i).Name = words(0)
                    pts(i).North = CDbl(words(1))
                    pts(i).East = CDbl(words(2))
                    pts(i).Height = CDbl(words(3))
                    If (UBound(words) = 4)
                        pts(i).Descr = words(4)
                    End If
                Else
                    pts(i).Name = ""
                    pts(i).North = CDbl(words(0))
                    pts(i).East = CDbl(words(1))
                    pts(i).Height = CDbl(words(2))
                    If (UBound(words) = 3)
                        pts(i).Descr = words(3)
                    End If
                End If
            Catch ex As Exception
                If Me.CK_WithNr.Checked = True Then
                    MsgBox(lines(i) & vbNewLine & _
                       CMain._T(tx.PT3D_ERPRS), _
                       MsgBoxStyle.Critical or MsgBoxStyle.ApplicationModal, _
                       CMain._T(tx.ERR))
                    Return 0
                Else
                    MsgBox(lines(i) & vbNewLine & _
                       CMain._T(tx.PT3D_ERPRS), _
                       MsgBoxStyle.Critical or MsgBoxStyle.ApplicationModal, _
                       CMain._T(tx.ERR))
                    Return 0
                End If
            End Try
        Next i
        Return Ubound(pts) + 1
    End Function
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub Cancel_Button_Click(ByVal sender As System.Object, _
                                    ByVal e As System.EventArgs) _
                                    Handles Cancel_Button.Click
        

        Try
            CM_3DPoints.Stgs.dSaveZ = CDbl(Me.TX_Height.Text)
        Catch ex As Exception
        End Try
        CM_3DPoints.Stgs.sNameSave = Me.TX_NrFirst.Text

        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub CD_PolyPt_Load( _
        ByVal sender As System.Object,  _
        ByVal e As System.EventArgs) _
        Handles MyBase.Load

        Me.Text = CMain._T(tx.POLYPTS) & ". "
        Me.B_Create.Text = CMain._T(TX.CREATE)
        Me.B_Pick.Text = CMain._T(TX.PICK)
        Me.B_Save.Text = CMain._T(TX.SAVE)
        Me.Cancel_Button.Text = CMain._T(TX.CLOSE)
        Me.CK_WithNr.Text = CMain._T(TX.PTS_WITHNR)
        Me.B_Pick.Text = CMain._T(TX.PICK)
        Me.B_Settings.Text = CMain._T(TX.SETTINGS)
        Me.CK_NonUniq.Text = CMain._T(TX.ALOWNQPT)


        Me.TX_Height.Text = CM_3DPoints.Stgs.dSaveZ.ToString
        Me.TX_NrFirst.Text = CM_3DPoints.Stgs.sNameSave

        Me.CK_WithNr.Checked = CM_3DPoints.Stgs.bUsePtNr
        Me.RB_FromPick.Checked = CM_3DPoints.Stgs.bHFromPoly
        Me.CK_NonUniq.Checked = CM_3DPoints.Stgs.bAllowNonUniq

        TX_NrFirst.Enabled = CK_WithNr.Checked
        TX_Height.Enabled = RB_UseThis.Checked
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub PickPoints( _
        ByRef acDoc As Document, _
        ByRef acTrans As Transaction, _
        ByRef iFName As Integer, _
        ByRef pts() As FSK_PT3D, _
        ByRef ptcnt As Integer, _
        ByRef strZ As String _
        )
        Dim sForm As String
        If (String.IsNullOrEmpty(CM_3DPoints.Stgs.sFormCoord)) Then
            sForm = "0.000"
        Else
            sForm = CM_3DPoints.Stgs.sFormCoord
        End If

        Dim pct As PtMg3D
        Dim pt As FSK_PT3D = Nothing
        pct.iAlc = 4
        pct.iUsd = 0
        ReDim pct.pt(0 to pct.iAlc-1)

		Dim ppo As PromptPointOptions
		Dim ppr As PromptPointResult
        Dim arg(0 To 3) As Object
        Dim ifTmp As Integer = iFName

		ppo = New PromptPointOptions(vbLf & CMain._T(TX.PICKPOINT) & ":")
        Do
			ppr = acDoc.Editor.GetPoint(ppo)
		    If ppr.Status = PromptStatus.OK Then
                For j As Integer = 0 to ptcnt - 1
                    If Math.Abs(pts(j).North - ppr.Value.Y) < CMain.MARGIN Then
                    If Math.Abs(pts(j).East - ppr.Value.X) < CMain.MARGIN Then
                        CMain.InformUser(TX.ADDPT_EXST)
                        Continue Do
                    End If
                    End If
                Next j
                For j As Integer = 0 to pct.iUsd - 1
                    If Math.Abs(pct.pt(j).North - ppr.Value.Y) < CMain.MARGIN Then
                    If Math.Abs(pct.pt(j).East - ppr.Value.X) < CMain.MARGIN Then
                        CMain.InformUser(TX.ADDPT_EXST)
                        Continue Do
                    End If
                    End If
                Next j
                If ifTmp = Integer.MaxValue Then
                    arg(0) = " "
                Else
                    arg(0) = ifTmp.ToString
                End If
                pt.North = ppr.Value.Y
                pt.East = ppr.Value.X
                pt.Height = ppr.Value.Z
                pt.Name = CStr(arg(0))
                CMain.AddPtToChain3D(pct, pt)

                arg(1) = ppr.Value.Y
                arg(2) = ppr.Value.X
                arg(3) = ppr.Value.Z
                CMain.InformUser(String.Format(CMain._T(TX.ADDPT_INF),arg))
                ifTmp = ifTmp + 1
            Else
                Exit Do
            End If

        Loop

        Dim acPtMin As FSK_PT
        Dim acPtMax As FSK_PT
        Dim iAdded As Integer = pct.iUsd

        '// we have a list of unique points
        '// we now search the point that has
        '// the angle closest to -PI/4

        Dim ang As Double = Math.Atan2( _
            pct.pt(0).North - acPtMax.N_1, _
            pct.pt(0).East - acPtMin.E_1)
        Dim dst As Double = Math.Sqrt( _
            Math.Pow(pct.pt(0).North - acPtMax.N_1, 2) + _
            Math.Pow(pct.pt(0).East - acPtMin.E_1, 2))
        Dim dMinA As Double = Math.Abs((ang + (Math.PI / 4)) * (dst * 10))

        Dim dCrtA As Double
        Dim iMinA As Integer = 0
        For i = 1 to iAdded-1
            ang = Math.Atan2( _
                pct.pt(i).North - acPtMax.N_1, _
                pct.pt(i).East - acPtMin.E_1)
            dst = Math.Sqrt( _
                Math.Pow(pct.pt(i).North - acPtMax.N_1, 2) + _
                Math.Pow(pct.pt(i).East - acPtMin.E_1, 2))
            dCrtA = Math.Abs((ang + (Math.PI / 2)) * (dst * 10))
            If dCrtA < dMinA Then
                dCrtA = dMinA
                iMinA = i
            End If
        Next i
        '// there, this is first
        If Me.CK_WithNr.Checked = True Then
            For i = iMinA to iAdded-1
                If me.RB_FromPick.Checked Then
                    strZ = pct.pt(i).Height.ToString(sForm)
                End If

                Me.TX_ListOfPoints.Text = Me.TX_ListOfPoints.Text & _
                    iFName.ToString & vbTab & _
                        pct.pt(i).North.ToString(sForm) & vbTab & _
                        pct.pt(i).East.ToString(sForm) & vbTab & _
                        strZ & vbNewLine
                iFName = iFName + 1
            Next i
            For i = 0 to iMinA - 1
                If me.RB_FromPick.Checked Then
                    strZ = pct.pt(i).Height.ToString(sForm)
                End If
                Me.TX_ListOfPoints.Text = Me.TX_ListOfPoints.Text & _
                    iFName.ToString & vbTab & _
                        pct.pt(i).North.ToString(sForm) & vbTab & _
                        pct.pt(i).East.ToString(sForm) & vbTab & _
                        strZ & vbNewLine
                iFName = iFName + 1
            Next
            Me.TX_NrFirst.Text = iFName.ToString
        Else
            For i = iMinA to iAdded-1
                If me.RB_FromPick.Checked Then
                    strZ = pct.pt(i).Height.ToString(sForm)
                End If
                Me.TX_ListOfPoints.Text = Me.TX_ListOfPoints.Text & _
                        pct.pt(i).North.ToString(sForm) & vbTab & _
                        pct.pt(i).East.ToString(sForm) & vbTab & _
                        strZ & vbNewLine
                iFName = iFName + 1
            Next i
            For i = 0 to iMinA - 1
                If me.RB_FromPick.Checked Then
                    strZ = pct.pt(i).Height.ToString(sForm)
                End If
                Me.TX_ListOfPoints.Text = Me.TX_ListOfPoints.Text & _
                        pct.pt(i).North.ToString(sForm) & vbTab & _
                        pct.pt(i).East.ToString(sForm) & vbTab & _
                        strZ & vbNewLine
                iFName = iFName + 1
            Next
        End If



    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub PickPoly( _
        ByRef acDoc As Document, _
        ByRef acTrans As Transaction, _
        ByRef iFName As Integer, _
        ByRef pts() As FSK_PT3D, _
        ByRef ptcnt As Integer, _
        ByRef strZ As String _
        )

        Dim sForm As String
        If (String.IsNullOrEmpty(CM_3DPoints.Stgs.sFormCoord)) Then
            sForm = "0.000"
        Else
            sForm = CM_3DPoints.Stgs.sFormCoord
        End If

        Dim acPoly As Polyline = Nothing

        '// have the user select
        If CMain.SelectPolyline( _
            acTrans, _
            acDoc, _
            acPoly, _
            True, True) = True Then
            
            Dim pts1(0 To acPoly.NumberOfVertices-1) As FSK_PT
            Dim iAdded As Integer = 1
            Dim acPt2 As Point2d
            
            Dim acPtMin As FSK_PT
            Dim acPtMax As FSK_PT
            If RB_FromPick.Checked = True Then
                strZ = acPoly.Elevation.ToString(sForm)
            End If

            acPt2 = acPoly.GetPoint2dAt(0)
            pts1(0).N_1 = acPt2.Y
            pts1(0).E_1 = acPt2.X
            acPtMin.N_1 = acPt2.Y
            acPtMax.N_1 = acPt2.Y
            acPtMin.E_1 = acPt2.X
            acPtMax.E_1 = acPt2.X
            For i = 1 to acPoly.NumberOfVertices-1
                acPt2 = acPoly.GetPoint2dAt(i)
                For j = 0 to iAdded - 1
                    If Math.Abs(pts1(j).N_1 - acPt2.Y) < CMain.MARGIN Then
                    If Math.Abs(pts1(j).E_1 - acPt2.X) < CMain.MARGIN Then
                        GoTo NextInFor
                    End If
                    End If
                Next
                For j = 0 to ptcnt - 1
                    If Math.Abs(pts(j).North - acPt2.Y) < CMain.MARGIN Then
                    If Math.Abs(pts(j).East - acPt2.X) < CMain.MARGIN Then
                        GoTo NextInFor
                    End If
                    End If
                Next
                pts1(iAdded).N_1 = acPt2.Y
                pts1(iAdded).E_1 = acPt2.X
                iAdded = iAdded + 1
                if acPtMin.N_1 > acPt2.Y Then
                    acPtMin.N_1 = acPt2.Y
                End If
                if acPtMax.N_1 < acPt2.Y Then
                    acPtMax.N_1 = acPt2.Y
                End If
                if acPtMin.E_1 > acPt2.X Then
                    acPtMin.E_1 = acPt2.X
                End If
                if acPtMax.E_1 < acPt2.X Then
                    acPtMax.E_1 = acPt2.X
                End If
                
                NextInFor:
            Next i

            '// we have a list of unique points
            '// we now search the point that has
            '// the angle closest to -PI/4

            Dim ang As Double = Math.Atan2( _
                pts1(0).N_1 - acPtMax.N_1, _
                pts1(0).E_1 - acPtMin.E_1)
            Dim dst As Double = Math.Sqrt( _
                Math.Pow(pts1(0).N_1 - acPtMax.N_1, 2) + _
                Math.Pow(pts1(0).E_1 - acPtMin.E_1, 2))
            Dim dMinA As Double = Math.Abs((ang + (Math.PI / 4)) * (dst * 10))

            Dim dCrtA As Double
            Dim iMinA As Integer = 0
            For i = 1 to iAdded-1
                ang = Math.Atan2( _
                    pts1(i).N_1 - acPtMax.N_1, _
                    pts1(i).E_1 - acPtMin.E_1)
                dst = Math.Sqrt( _
                    Math.Pow(pts1(i).N_1 - acPtMax.N_1, 2) + _
                    Math.Pow(pts1(i).E_1 - acPtMin.E_1, 2))
                dCrtA = Math.Abs((ang + (Math.PI / 2)) * (dst * 10))
                If dCrtA < dMinA Then
                    dCrtA = dMinA
                    iMinA = i
                End If
            Next i
            '// there, this is first
            If Me.CK_WithNr.Checked = True Then
                For i = iMinA to iAdded-1
                    Me.TX_ListOfPoints.Text = Me.TX_ListOfPoints.Text & _
                        iFName.ToString & vbTab & _
                            pts1(i).N_1.ToString(sForm) & vbTab & _
                            pts1(i).E_1.ToString(sForm) & vbTab & _
                            strZ & vbNewLine
                    iFName = iFName + 1
                Next i
                For i = 0 to iMinA - 1
                    Me.TX_ListOfPoints.Text = Me.TX_ListOfPoints.Text & _
                        iFName.ToString & vbTab & _
                            pts1(i).N_1.ToString(sForm) & vbTab & _
                            pts1(i).E_1.ToString(sForm) & vbTab & _
                            strZ & vbNewLine
                    iFName = iFName + 1
                Next
                Me.TX_NrFirst.Text = iFName.ToString
            Else
                For i = iMinA to iAdded-1
                    Me.TX_ListOfPoints.Text = Me.TX_ListOfPoints.Text & _
                            pts1(i).N_1.ToString(sForm) & vbTab & _
                            pts1(i).E_1.ToString(sForm) & vbTab & _
                            strZ & vbNewLine
                Next i
                For i = 0 to iMinA - 1
                    Me.TX_ListOfPoints.Text = Me.TX_ListOfPoints.Text & _
                            pts1(i).N_1.ToString(sForm) & vbTab & _
                            pts1(i).E_1.ToString(sForm) & vbTab & _
                            strZ & vbNewLine
                Next
            End If

        End If
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub B_Pick_Click( _
        ByVal sender As System.Object,  _
        ByVal e As System.EventArgs) _
        Handles B_Pick.Click

        Dim iFName As Integer
        Dim strZ As String = Nothing
        Dim pts() As FSK_PT3D = Nothing
        Dim ptcnt As Integer = 0
        If CK_NonUniq.Checked = False Then
            If String.IsNullOrEmpty(Me.TX_ListOfPoints.Text.Trim) _
                     = False Then
                ptcnt = GetPointList(pts)
                If ptcnt = 0 then Return
            End If
        End If
        iFName = Integer.MaxValue
        If Me.CK_WithNr.Checked = True Then
            Try
                iFName = CInt(Me.TX_NrFirst.Text)
            Catch ex As System.Exception
                MsgBox("<" & Me.TX_NrFirst.Text & ">" & vbNewLine & CMain._T(tx.NAN), _
                       MsgBoxStyle.Critical or MsgBoxStyle.ApplicationModal, _
                       CMain._T(tx.ERR))
                Me.TX_NrFirst.SelectAll()
                Me.TX_NrFirst.Focus()
                Return
            End Try
        End If
        If RB_FromPick.Checked = False Then
            strZ = Me.TX_Height.Text
        End If

        Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument

        Me.Hide

        Using acLock As DocumentLock = acDoc.LockDocument()
        Using acTrans As Transaction = _
            acDoc.Database.TransactionManager.StartTransaction()
            Try
                '// --------------------------------------------------
                '// Get Model space (is just a block)
                Dim acBlkModel As BlockTableRecord = DirectCast( _
                    acTrans.GetObject( _
                        acDoc.Database.CurrentSpaceId, _
                        OpenMode.ForWrite),  _
                    BlockTableRecord)
                
                '// \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
                If (Windows.Forms.Control.ModifierKeys And _
                    Windows.Forms.Keys.Control) <> 0 Then

                    PickPoints(acDoc, acTrans, iFName, pts, ptcnt, strZ)
                Else

                    PickPoly(acDoc, acTrans, iFName, pts, ptcnt, strZ)
                End If

        

                
                acTrans.Abort()
            Catch ex As System.Exception
                acTrans.Abort()
                CMain.DebugException(ex)
            End Try
        End Using '// acTrans
        End Using '// acLock

        Autodesk.AutoCAD.ApplicationServices. _
        Application.ShowModelessDialog(Me)
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub B_Create_Click( _
        ByVal sender As System.Object,  _
        ByVal e As System.EventArgs) _
        Handles B_Create.Click

        Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
        Dim pts() As FSK_PT3D = Nothing
        Dim PtErrCnt as integer
        Dim ptcnt As Integer = GetPointList(pts)
        If (ptcnt = 0) Then Return
        Using acLock As DocumentLock = acDoc.LockDocument()
        Using acTrans As Transaction = _
            acDoc.Database.TransactionManager.StartTransaction()
            Try
                '// --------------------------------------------------
                '// Get Model space (is just a block)
                Dim acBlkModel As BlockTableRecord = DirectCast( _
                    acTrans.GetObject( _
                        acDoc.Database.CurrentSpaceId, _
                        OpenMode.ForWrite),  _
                    BlockTableRecord)
                For i As Integer = 0 to ptcnt - 1
                    Dim BCrRet As Boolean
                    If (CM_3DPoints.Stgs.doBlock) Then
                        BCrRet = CM_3DPoints.BlockPoint( _
                            acDoc, _
                            pts(i), _
                            CM_3DPoints.Stgs)
                    Else
                        BCrRet = CM_3DPoints.EntityPoint( _
                            acDoc, _
                            pts(i), _
                            CM_3DPoints.Stgs)
                    End If
                    If (BCrRet = False) Then
                        If (CM_3DPoints.Stgs.bIgnErr) Then
                            PtErrCnt = PtErrCnt + 1
                            GoTo NextLine
                        End If
                        CMain.AlertUser( _
                        CMain._T(TX.PT3D_ECREATE) & "!" & vbNewLine & _
                        CMain._T(TX.LINE) & ": <" & _
                        i.ToString() & ">")
                        Exit Try
                    End If
                    PtCnt = PtCnt + 1
                    NextLine:
                Next

                If (CM_3DPoints.Stgs.bIgnErr And PtErrCnt <> 0) Then
                    CMain.AlertUser( _
                            String.Format(CMain._T(TX.PT3D_IMPOK), _
                                          PtCnt) & vbNewLine & _
                            String.Format(CMain._T(TX.PT3D_IMPFAIL), PtCnt) _
                    )
                Else
                    CMain.InformUser(String.Format(CMain._T(TX. _
                                                 PT3D_IMPOK), PtCnt))
                End If

                acTrans.Commit()
            Catch ex As System.Exception
                acTrans.Abort()
                CMain.DebugException(ex)
            End Try
        End Using '// acTrans
        End Using '// acLock

    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub B_Save_Click( _
        ByVal sender As System.Object,  _
        ByVal e As System.EventArgs) _
        Handles B_Save.Click

        Dim sForm As String
        If (String.IsNullOrEmpty(CM_3DPoints.Stgs.sFormCoord)) Then
            sForm = "0.000"
        Else
            sForm = CM_3DPoints.Stgs.sFormCoord
        End If

        Dim objWRi As System.IO.StreamWriter = Nothing
        Dim ExcelSheet As XLS = Nothing
        Dim ExcelWorkbook As XLW = Nothing
        Dim pts() As FSK_PT3D = Nothing
        Dim i As Integer
        Dim iT As Integer = 0
    try
        Dim ptcnt As Integer = GetPointList(pts)
        If (ptcnt = 0) Then Return
        Dim fdlg As Windows.Forms.SaveFileDialog = _
            New Windows.Forms.SaveFileDialog()

        fdlg.Title = Me.Text
        fdlg.InitialDirectory = "c:\"
        fdlg.Filter = CMain._T(tx.EXCFILES) & " (*.xls)|*.xls|" & _
                      CMain._T(tx.TXTFILES) & " (*.txt)|*.txt|" & _
                      CMain._T(tx.ALLFILES) & " (*.*)|*.*"
        fdlg.FilterIndex = 2
        fdlg.RestoreDirectory = False
        fdlg.FileName = CM_3DPoints.Stgs.LastExpFile
        If fdlg.ShowDialog() = Windows.Forms.DialogResult.OK Then
            CM_3DPoints.Stgs.LastExpFile = fdlg.FileName
            i = CM_3DPoints.Stgs.LastExpFile.LastIndexOf(".")
            If (CM_3DPoints.Stgs.LastExpFile.Substring(i+1).ToUpper = "XLS") Then
                '// export to an excel file

                If System.IO.File.Exists(CM_3DPoints.Stgs.LastExpFile) Then
                    ExcelWorkbook = New XLW(CM_3DPoints.Stgs.LastExpFile)
                Else
                    ExcelWorkbook = New XLW()
                    If (ExcelWorkbook.ErrSts <> XLW.ERR.OK) Then
                        CMain.AlertUser(TX.EWRITE)
                        Exit Try
                    End If
                    ExcelWorkbook.SaveAs(CM_3DPoints.Stgs.LastExpFile)
                End If
                If (ExcelWorkbook Is Nothing) Then
                    CMain.AlertUser(TX.EWRITE)
                    Exit Try
                End If
                ExcelSheet = ExcelWorkbook.GetActiveSheet()
                For i = 0 to ptcnt - 1
                    With ExcelSheet
                        .SetCV(i+1, 1, pts(i).Name)
                        .SetCV(i+1, 2, pts(i).North)
                        .SetCV(i+1, 3, pts(i).East)
                        .SetCV(i+1, 4, pts(i).Height)
                        .SetCV(i+1, 5, pts(i).Descr)
                    End With
                Next
                ExcelWorkbook.Save()
            Else
                '// export to a text file
                '// attempt to open a write stream
                Try
                    objWRi = New System.IO.StreamWriter( _
                        CM_3DPoints.Stgs.LastExpFile, False)
                Catch
                End Try
                If (objWRi Is Nothing) Then
                    CMain.AlertUser(TX.EWRITE)
                    Exit Try
                End If
                
                For i = 0 to ptcnt - 1
                    objWRi.WriteLine( _
                     pts(i).Name & vbTab & _
                     pts(i).North.ToString(sForm) & vbTab & _
                     pts(i).East.ToString(sForm) & vbTab & _
                     pts(i).Height.ToString(sForm) & vbTab & _
                     pts(i).Descr)
                Next

            End If
            iT = ptcnt
        End If
        CMain.InformUser(String.Format(CMain._T(TX.PT3D_EXPOK), iT))

    Catch ex As System.Exception
        CMain.DebugException(ex)
        CMain.PopUpExc()
    Finally
        If (Not objWRi Is Nothing) Then
            objWRi.Close()
        ElseIf Not (ExcelWorkbook Is Nothing) Then
            Call ExcelWorkbook.Close(False)
            ExcelWorkbook = Nothing
            ExcelSheet = Nothing
        End If
    End Try
        pts = Nothing

    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub B_Settings_Click( _
        ByVal sender As System.Object,  _
        ByVal e As System.EventArgs) _
        Handles B_Settings.Click

        Dim StgD As New CST_3DPoints
        Autodesk.AutoCAD.ApplicationServices. _
            Application.ShowModalDialog(StgD)
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub RB_UseThis_CheckedChanged( _
        ByVal sender As System.Object,  _
        ByVal e As System.EventArgs) _
        Handles RB_UseThis.CheckedChanged

        TX_Height.Enabled = RB_UseThis.Checked
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub CK_WithNr_CheckedChanged( _
        ByVal sender As System.Object,  _
        ByVal e As System.EventArgs) _
        Handles CK_WithNr.CheckedChanged

        TX_NrFirst.Enabled = CK_WithNr.Checked
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub CD_PolyPt_FormClosing( _
        ByVal sender As System.Object,  _
        ByVal e As System.Windows.Forms.FormClosingEventArgs) _
        Handles MyBase.FormClosing

        Try
            CM_3DPoints.Stgs.dSaveZ = cdbl(Me.TX_Height.Text)
        Catch ex As Exception
        End Try
        CM_3DPoints.Stgs.sNameSave = Me.TX_NrFirst.Text
        CM_3DPoints.Stgs.bUsePtNr = Me.CK_WithNr.Checked
        CM_3DPoints.Stgs.bHFromPoly =  Me.RB_FromPick.Checked
        CM_3DPoints.Stgs.bAllowNonUniq = Me.CK_NonUniq.Checked

    End Sub
    '// ===================================================================== '


    Private Sub B_Sync_Click( _
        ByVal sender As System.Object,  _
        ByVal e As System.EventArgs) _
        Handles B_Sync.Click

        Dim sForm As String
        If (String.IsNullOrEmpty(CM_3DPoints.Stgs.sFormCoord)) Then
            sForm = "0.000"
        Else
            sForm = CM_3DPoints.Stgs.sFormCoord
        End If

        Dim pts() As FSK_PT3D = Nothing
        Dim i As Integer = GetPointList(pts)
        If i > 0 Then
        Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
        Using acLock As DocumentLock = acDoc.LockDocument()
            CM_3DPoints.MatchPoints(acDoc, pts)
            Me.TX_ListOfPoints.Text = ""
            For i = 0 to ubound(pts)
                Me.TX_ListOfPoints.Text = Me.TX_ListOfPoints.Text & _
                        pts(i).Name & vbTab & _
                        pts(i).North.ToString(sForm) & vbTab & _
                        pts(i).East.ToString(sForm) & vbTab & _
                        pts(i).Height.ToString(sForm) & vbNewLine
            Next i

        End Using
        End if
    End Sub
End Class


'//  CLASS    =============================================================== '
'//
'//
'//
'//
'//  FUNCTIONS    ----------------------------------------------------------- '

'//  FUNCTIONS    =========================================================== '
'//
'//
'//
'//
'// ------------------------------------------------------------------------- '
'// ========================================================================= '
